package utils.common;


import com.sun.deploy.net.*;
import org.apache.http.*;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by whyang on 2017/06/05.
 */

public class HttpClient2 {

    public static void main(String args[]) throws IOException {

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("account", "wwwwww"));
        formparams.add(new BasicNameValuePair("password", "www"));
        HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");

        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .build();

//        HttpClient client = new DefaultHttpClient();
//        HttpPost client = new HttpPost();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpHost proxy = new HttpHost("127.0.0.1", 5389);
        client = HttpClients.custom().setProxy(proxy).build();

        HttpResponse resp;


        if(false){
            HttpPost req = /*(HttpPost)*/new HttpPost("http://m.ctrip.com/login");
            req.setEntity(reqEntity);
            req.setConfig(requestConfig);
            req.setHeader("key", "value");
            req.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1));
            URI uri = null;
            try {
                 uri = new URI("m.ctrip.com");
            } catch (URISyntaxException urise){
            }
            req.setURI(uri);
            HttpHost host = new HttpHost("m.ctrip.com", 80);

            resp = client.execute(req);
        } else {
            String url1 = "http://m.ctrip.com";
            String url2 = "http://m.baidu.com";
            HttpGet req = new HttpGet(url1);
            req.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1));

            resp = client.execute(req);
//            resp = client.execute(proxy, req);
        }

        if (resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity resEntity = resp.getEntity();
            String message = EntityUtils.toString(resEntity, "utf-8");
            System.out.println(message);
        } else {
            System.out.println("请求失败");
        }
    }

//    HttpEntityEnclosingRequestBase reqBase;
//    HttpEntityEnclosingRequest req;
//    HttpEntity reqEnty;

    /******
     * HttpClient clt;
         <---    CloseableHttpClient;
                     <---    AbstractHttpClient;
                                <---    DefaultHttpClient
                     <---    InternalHttpClient;
                     <---    MinimalHttpClient;
     *
     * ***/


//    HttpClients clts;

    /****
     *HttpRequestRetryHandler reqRetryhndl;
         <---    DefaultHttpRequestRetryHandler;
                     <---    StandardHttpRequestRetryHandler;
     * **/

    HttpExecutionAware httpReqAwr;

    /*
    ClientExecChain cltExecChain;
        <---    MainClientExec;
        <---    ProtocolExec;
        <---    BackoffStrategyExec;
        <---    MinimalClientExec;
        <---    RedirectExec;
        <---    RetryExec;
        <---    ServiceUnavailableRetryExec;

    HttpRequestExecutor
    */

    /*
    HttpRequestExecutor httpReqExctr;
    MainClientExec MainCltExc;

    HttpConnection httpcnnt;
        <---    HttpClientConnection htpcltcnct;
                <---    OperatedClientConnection oprCltCnnt;
                <---    ManagedClientConnection mngdCltCnnt;
        <---    HttpInetConnection;
        <---    BHttpConnectionBase;
                <---    DefaultBHttpClientConnection;
                            <---    DefaultManagedHttpClientConnection;
                                    <---    LoggingManagedHttpClientConnection;

                <---    DefaultBHttpServerConnection;

    ClientConnectionRequest cltConntReq;

    AbstractHttpClient absHttpClt;

    HttpRequestBase;
    HttpResponse;
    HttpResponseFactory;
    */

    /*
    HttpClientConnectionManager;

    HttpRoutePlanner httpRoutePlnr;
        <---    DefaultRoutePlanner;
                <---    DefaultProxyRoutePlanner;
                <---    SystemDefaultRoutePlanner;



    RouteSpecificPool routeSpclPool;

    HttpClientBuilder httpCltBldr;

    RequestConfig reqCfg;
    */


//    HttpProcessor httpPrcsr;
//    MainClientExec mainCltExc;
//    HttpRouteDirector httpRouteDrct;


    @Test
    public void testOne(){

        HttpPost reqX = new HttpPost("http://www.ctrip.com");
        HttpGet reqG = new HttpGet("http://www.baidu.com");
        reqG.getMethod();

        reqX.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1));

        CloseableHttpClient client = HttpClientBuilder.create().build();

        try {
            HttpResponse resp;
            if (false){
                /*HttpResponse */resp = client.execute(reqG);
            } else {
                /*HttpResponse */resp = (HttpResponse)client.execute(reqX);
            }

            if (resp.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = resp.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                System.out.println(message);
            } else {
                System.out.println("请求失败");
            }
        } catch (IOException ioe){

        }

    }

}
